package com.bugstack.domain.activity.service.quota;

import com.alibaba.fastjson.JSON;
import com.bugstack.domain.activity.model.aggregate.CreateOrderAggregate;
import com.bugstack.domain.activity.model.entity.*;
import com.bugstack.domain.activity.reopsitory.IActivityRepository;
import com.bugstack.domain.activity.service.quota.policy.ITradePolicy;
import com.bugstack.domain.activity.service.quota.rule.IActionChain;
import com.bugstack.domain.activity.service.quota.rule.factory.DefaultActivityChainFactory;
import com.bugstack.types.enums.ResponseCode;
import com.bugstack.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @Author: czb
 * @Description: 活动模版类
 * @DateTime: 2025/3/21 14:05
 **/
@Slf4j
public abstract class AbstractRaffleQuotaQuotaActivityQuota extends RaffleActivityQuotaSupport implements IRaffleAccountQuotaService {

    private final Map<String, ITradePolicy> tradePolicyMap;

    public AbstractRaffleQuotaQuotaActivityQuota(IActivityRepository activityRepository, DefaultActivityChainFactory defaultActivityChainFactory, Map<String, ITradePolicy> tradePolicyMap) {
        super(activityRepository, defaultActivityChainFactory);
        this.tradePolicyMap = tradePolicyMap;
    }

    @Override
    public UnpaidActivityOrderEntity createOrder(SkuRechargeEntity skuRechargeEntity) {
        //1.参数校验
        Long sku = skuRechargeEntity.getSku();
        String outBusinessNo = skuRechargeEntity.getOutBusinessNo();
        String userId = skuRechargeEntity.getUserId();
        if (null == sku || StringUtils.isBlank(outBusinessNo) || StringUtils.isBlank(userId)) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        //2.查询未支付订单
        UnpaidActivityOrderEntity noPaidOrder = queryNoPaidOrder(skuRechargeEntity);
        if (null != noPaidOrder) return noPaidOrder;

        //3.查询基础信息
        //查询sku
        ActivitySkuEntity activitySkuEntity = queryActivitySku(skuRechargeEntity.getSku());
        //查询活动
        ActivityEntity activityEntity = queryRaffleActivityByActivityId(activitySkuEntity.getActivityId());
        //查询库存
        ActivityCountEntity activityCountEntity = queryRaffleActivityCountByActivityCountId(activitySkuEntity.getActivityCountId());
        log.info("查询结果：{} {} {}", JSON.toJSONString(activitySkuEntity), JSON.toJSONString(activityEntity), JSON.toJSONString(activityCountEntity));

        //4.活动规则校验
        IActionChain actionChain = defaultActivityChainFactory.openActionChain();
        actionChain.action(activitySkuEntity, activityEntity, activityCountEntity);

        //5.构建订单聚合对象
        CreateOrderAggregate createOrderAggregate = buildOrderAggregate(skuRechargeEntity, activitySkuEntity, activityEntity, activityCountEntity);

        // 6. 保存订单
        ITradePolicy tradePolicy = tradePolicyMap.get(skuRechargeEntity.getOrderTradeType().getCode());
        tradePolicy.trade(createOrderAggregate);

        //7.返回对象
        return UnpaidActivityOrderEntity.builder()
                .orderId(createOrderAggregate.getActivityOrderEntity().getOrderId())
                .outBusinessNo(createOrderAggregate.getActivityOrderEntity().getOutBusinessNo())
                .userId(userId)
                .payAmount(createOrderAggregate.getActivityOrderEntity().getPayAmount())
                .build();
    }

    protected abstract CreateOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity);


}
